package com.ruoyi.house_sale.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.house_sale.domain.Finance;
import com.ruoyi.house_sale.domain.Houseinfo;
import com.ruoyi.house_sale.service.IBuyHouseService;
import com.ruoyi.house_sale.service.IHouseinfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.Commmision;
import com.ruoyi.house_sale.service.ICommmisionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 佣金管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/house_sale/Commmision")
public class CommmisionController extends BaseController
{
    @Autowired
    private ICommmisionService commmisionService;
    @Autowired
    private IBuyHouseService buyHouseService;
    @Autowired
    private IHouseinfoService houseinfoService;

    /**
     * 查询佣金管理列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commmision commmision)
    {
        startPage();
        List<Commmision> list = commmisionService.selectCommmisionList(commmision);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:list1')")
    @GetMapping("/list1/{buyid}")
    public TableDataInfo list1(@PathVariable("buyid") Long  buyid)
    {
        startPage();
        List<Commmision> list = commmisionService.selectCommmisionByBuyId(buyid);
        return getDataTable(list);

    }
    @GetMapping("/selectCommmision/{buyid}")
    public AjaxResult selectCommmision(@PathVariable("buyid") Long buyid){

        BigDecimal commisionpaid = commmisionService.selectCommmision(buyid);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("commisionpaid", commisionpaid);

        return ajax;
    }
    /**
     * 导出佣金管理列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:export')")
    @Log(title = "佣金管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Commmision commmision)
    {
        List<Commmision> list = commmisionService.selectCommmisionList(commmision);
        ExcelUtil<Commmision> util = new ExcelUtil<Commmision>(Commmision.class);
        util.exportExcel(response, list, "佣金管理数据");
    }

    /**
     * 获取佣金管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:query')")
    @GetMapping(value = "/{commisionid}")
    public AjaxResult getInfo(@PathVariable("commisionid") Long commisionid)
    {
        return AjaxResult.success(commmisionService.selectCommmisionByCommisionid(commisionid));
    }

    /**
     * 新增佣金管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:add')")
    @Log(title = "佣金管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commmision commmision)
    {
        Long hourseid = buyHouseService.selecthouserid(commmision.getBuyid());
        Houseinfo houseInfo = new Houseinfo();
        houseInfo.setHourseid(hourseid);
//        BigDecimal yfmoney = commmisionService.selectCommmision(commmision.getBuyid());
        BigDecimal yfmoney = commmision.getCommisionpaid();
        BigDecimal yfmoney1 = commmision.getPaidmoney();
        houseInfo.setCommisionpaid(yfmoney.add(yfmoney1));
        houseinfoService.updatecommisionpaid(houseInfo);
        return toAjax(commmisionService.insertCommmision(commmision));
    }

    /**
     * 修改佣金管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:edit')")
    @Log(title = "佣金管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commmision commmision)
    {
        return toAjax(commmisionService.updateCommmision(commmision));
    }

    /**
     * 删除佣金管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Commmision:remove')")
    @Log(title = "佣金管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commisionids}")
    public AjaxResult remove(@PathVariable Long[] commisionids)
    {
        return toAjax(commmisionService.deleteCommmisionByCommisionids(commisionids));
    }
    //作废
    @PutMapping( "/invalid")
    public AjaxResult invalid(@RequestBody Commmision form2)
    {
//        form2.
//                commmisionService
        return toAjax(commmisionService.beInvalid(form2));

    }
    @GetMapping( "/updmoney/{commisionid}")
    public AjaxResult updmoney(@PathVariable("commisionid") Long commisionid)
    {
        Commmision commmision =  commmisionService.selectCommmisionByCommisionid(commisionid);
        Long hourseid = buyHouseService.selecthouserid(commmision.getBuyid());
        Houseinfo houseInfo = houseinfoService.selectHouseinfoByHourseid(hourseid);
//        houseInfo.setHourseid(hourseid);
        BigDecimal yfmoney = houseInfo.getCommisionpaid();
        BigDecimal yfmoney1 = commmision.getPaidmoney();
        houseInfo.setCommisionpaid(yfmoney.subtract(yfmoney1));
        houseinfoService.updatecommisionpaid(houseInfo);
        commmision.setCommisionpaid(houseInfo.getCommisionpaid());
//        return toAjax(houseinfoService.updatecommisionpaid(houseInfo));
        return AjaxResult.success(commmisionService.selectCommmisionByCommisionid(commisionid));
    }
}
